package com.xyc.student.controller;

import com.xyc.student.mapper.AssociateMapper;
import com.xyc.student.mapper.PageMapper;
import com.xyc.student.mapper.StuMapper;
import com.xyc.student.mapper.TeacherMapper;
import com.xyc.student.pojo.Page;
import com.xyc.student.pojo.Stu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StuController {
    @Autowired
    StuMapper stuMapper;

    @Autowired
    PageMapper pageMapper;

    @Autowired
    TeacherMapper teacherMapper;

    @Autowired
    AssociateMapper associateMapper;

    //总列表
    @GetMapping("/stus")
    public String stus(Model model) {  // 首页  显示0-size
        Map<String, Object> map = new HashMap<>();
        Page page = pageMapper.find(1);
        map.put("from", 0);
        map.put("to", 6);
        List<Stu> stuList = stuMapper.pageLimit(map);
        model.addAttribute("stu", stuList);
        return "stu/list";
    }

    //来到编辑页面
    @GetMapping("/stu/edit/{id}")
    public String edit(@PathVariable("id") Integer id, Model model) {
        Stu stu = stuMapper.findById(id);
        model.addAttribute("stu", stu);
        return "stu/edit";
    }

    //编辑请求
    @RequestMapping("/stu/edit/ok")
    public String editOk(@RequestParam("id") Integer id,
                         @RequestParam("name") String name,
                         @RequestParam("cla") String cla,
                         @RequestParam("s1") Float s1,
                         @RequestParam("s2") Float s2,
                         @RequestParam("s3") Float s3,
                         @RequestParam("memo") String memo) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("name", name);
        map.put("cla", cla);
        map.put("s1", s1);
        map.put("s2", s2);
        map.put("s3", s3);
        float avg = (s1 + s2 + s3) / 3.0f;
        BigDecimal big = new BigDecimal(avg);
        map.put("avg", big.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());// 保留2位小数
        map.put("memo", memo);
        stuMapper.update(map);
        associateMapper.updateName(name, id); // 同步更新
        return "redirect:/stus";
    }

    //搜索返结果
    @RequestMapping("/search")
    public String search(@RequestParam("id") Integer id, Map<String, Object> map) {
        Stu stu = stuMapper.search(id);
        map.put("stu", stu);
        return "stu/result";
    }

    //来到添加页面
    @GetMapping("/add")
    public String add() {
        return "stu/add";
    }

    //添加
    @PostMapping("/stu")
    public String stu(Stu stu) {
        System.out.println("保存的学生信息:" + stu);
        Map<String, Object> map = new HashMap<>();
        //map.put("id",stu.getId());
        map.put("name", stu.getName());
        map.put("cla", stu.getCla());
        map.put("s1", stu.getS1());
        map.put("s2", stu.getS2());
        map.put("s3", stu.getS3());
        stuMapper.insert(map);
        Stu stu222 = stuMapper.findByName(stu.getName());  //由于id是自增，从添加到的记录中获取id
        associateMapper.add(stu.getName(), stu222.getId()); // 关联表同步 添加学生
        return "redirect:/stus";
    }

    //删除
    @RequestMapping("/stu/del/{id}")
    public String del(@PathVariable("id") Integer id) {
        stuMapper.del(id);
        associateMapper.del(id); // 关联表同步删除
        System.out.println("删除" + id + "号学生成绩");
        return "redirect:/stus";
    }

    //退出
    @RequestMapping("/exit")
    public String exit(HttpSession session, SessionStatus sessionStatus) {
        session.invalidate();
        sessionStatus.setComplete();
        return "login";
    }

    //批量删除
    @RequestMapping("/stu/dels")
    public String dels(HttpSession session) {
        System.out.println("测试 ids ");
        Object obj = session.getAttribute("ids");
        System.out.println(obj.toString());
        return "redirect:/stus";
    }

    //上一页
    @RequestMapping("/stu/page/prev")
    public String prev() {
        System.out.println("prev");
        return "redirect:/stus";
    }

    //下一页
    @RequestMapping("/stu/page/next")
    public String next(Model model) {
        System.out.println("next");   // 0-6   6-12    12-18
        Map<String, Object> map = new HashMap<>();
        map.put("from", 6);
        map.put("to", 12);
        List<Stu> stuList = stuMapper.pageLimit(map);

        model.addAttribute("stu", stuList); //新的list
        return "stu/list";
    }


}
